package org.smart4j.framework.utils;

import org.apache.commons.lang.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

/**
 * Created by Wenxin on 2017/2/26.
 */
public class StreamUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(StreamUtil.class);

    public static String getString(InputStream inputStream) {

        if (inputStream == null)
            return null;
        StringBuilder sb = new StringBuilder();
        byte[] b = new byte[1024];
        int len = 0;
        try {
            while ((len = inputStream.read()) != -1) {
                sb.append(ArrayUtils.subarray(b, 0, len));
            }
        } catch (IOException e) {
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("cast to String failure", e);
            throw new RuntimeException(e);
        }
        return sb.toString();
    }

}
